<template>
  <el-dialog
    :visible.sync="visible"
    :title="!dataForm.id ? $t('add') : $t('update')"
    :close-on-click-modal="false"
    :close-on-press-escape="false"
    width="30%"
    top="100px"
    :append-to-body="true"
  >
    <el-form
      :model="dataForm"
      :rules="dataRule"
      ref="dataForm"
      :label-width="$i18n.locale === 'en-US' ? '100px' : '100px'"
    >
      <el-row :gutter="10">
        <el-col :span="24">
          <el-form-item label="公告内容" prop="title">
            <el-input v-model="dataForm.title"></el-input>
          </el-form-item>
        </el-col>
        <!-- <el-col :span="12">
          <el-form-item label="类型" prop="type">
            <el-select v-model="dataForm.type" style="width: 100%">
              <el-option
                v-for="(item, index) in typeList"
                :key="index"
                :value="item.value"
                :label="item.name"
              ></el-option>
            </el-select>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="内容" prop="content">
            <editor-vue
              style="margin-top: -30px"
              :content="dataForm.content"
              @EditorChange="getEditor"
              ref="infoIntroduceRef"
            ></editor-vue>
          </el-form-item>
        </el-col> -->
      </el-row>
    </el-form>
    <template slot="footer">
      <el-button @click="cancel2()" v-if="type!=2">保存草稿</el-button>
      <el-button type="primary" @click="dataFormSubmitHandle()">{{
        $t("confirm")
      }}</el-button>
    </template>
  </el-dialog>
</template>

<script>
import debounce from "lodash/debounce";
import editorVue from "@/components/editor";
export default {
  data() {
    return {
      visible: false,
      dataForm: {
        id: "",
        title: "",
        content: "",
        type: "",
        isDraft: 0,
        status: 1,
      },
      typeList: [
        {
          name: "航运通知",
          value: 0,
        },
        {
          name: "其他信息",
          value: 1,
        },
      ],
    };
  },
  components: {
    editorVue,
  },
  props: ["type"],
  computed: {
    dataRule() {
      return {
        title: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        type: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
        content: [
          {
            required: true,
            message: this.$t("validate.required"),
            trigger: "blur",
          },
        ],
      };
    },
  },
  methods: {
    cancel2() {
      if (this.dataForm.id&&!this.type) {
        this.visible = false;
      } else {
        this.$confirm(`是否需要保存草稿`, this.$t("prompt.title"), {
          confirmButtonText: "需要",
          cancelButtonText: "不需要",
          type: "warning",
        })
          .then(() => {
            this.dataForm.isDraft = 1;
            this.dataFormSubmitHandle();
          })
          .catch(() => {
            this.visible = false;
          });
      }
    },
    getEditor(val) {
     // this.dataForm.content = val;
    },
    init() {
      this.visible = true;
      this.$nextTick(() => {
        this.$refs["dataForm"].resetFields();

        if (this.dataForm.id) {
          this.getInfo();
        } else {
          this.$nextTick(() => {
            this.$refs.infoIntroduceRef.init();
          });
        }
      });
    },
    //获取信息
    getInfo() {
      this.$http
        .get("/scenic/commnoticeo/" + this.dataForm.id)
        .then(({ data: res }) => {
          if (res.code !== 0) {
            return this.$message.error(res.msg);
          }
          this.dataForm = {
            ...this.dataForm,
            ...res.data,
          };
          this.$nextTick(() => {
           // this.$refs.infoIntroduceRef.init();
          });
        })
        .catch(() => {});
    },
    // 表单提交
    dataFormSubmitHandle: debounce(
      function () {
        this.$refs["dataForm"].validate((valid) => {
          if (!valid) {
            return false;
          }
          if (this.type == 2) {
            this.dataForm.isDraft = 1;
          }
          this.$http[!this.dataForm.id ? "post" : "put"](
            "/scenic/commnoticeo",
            this.dataForm
          )
            .then(({ data: res }) => {
              if (res.code !== 0) {
                return this.$message.error(res.msg);
              }
              this.$message({
                message: this.$t("prompt.success"),
                type: "success",
                duration: 500,
                onClose: () => {
                  this.visible = false;
                  this.$emit("refreshDataList");
                },
              });
            })
            .catch(() => {});
        });
      },
      1000,
      {
        leading: true,
        trailing: false,
      }
    ),
  },
};
</script>

<style lang="scss" scoped>
.txt {
  line-height: 40px;
  height: auto;
  margin: 0;
  padding: 0;

  &.content {
    ::v-deep p {
      padding: 0;
      line-height: 24px;
      margin: 0 !important;
    }

    ::v-deep img {
      width: 100%;
    }

    img {
      width: 100%;
    }
  }
}
</style>
